package cn.xiangyuanpin.web;

import cn.xiangyuanpin.pojo.Book;
import cn.xiangyuanpin.pojo.Page;
import cn.xiangyuanpin.service.BookService;
import cn.xiangyuanpin.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

public class BookServlet extends BaseServlet {

    private BookService bookService=new BookServiceImpl();

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name=req.getParameter("name");

        BigDecimal price=new BigDecimal(req.getParameter("price"));
        String author=req.getParameter("author");
        Integer sales=Integer.valueOf(req.getParameter("sales"));
        Integer  stock= Integer.valueOf(req.getParameter("stock"));

        Book book=new Book(null,name,author,price,sales,stock,null);
        bookService.addBook(book);
req.getRequestDispatcher("bookServlet?action=page").forward(req,resp);

    }
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pageNo=1;
        int pageSize=Page.PAGE_SIZE;
        try {
            pageNo=Integer.parseInt(req.getParameter("pageNo"));
        } catch (Exception e) {
//            e.printStackTrace();
        }
        try {
            pageSize=Integer.parseInt(req.getParameter("pageSize"));
        } catch (Exception e) {
//            e.printStackTrace();
        }

        Page<Book> page=bookService.page(pageNo,pageSize);

      req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);

    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Book>books=bookService.queryBooks();
        req.setAttribute("books",books);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);


    }




}
